// pages/find/find.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    findData: [],
    imgUrl: '',
    page:1,
    canGetList:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList(); 
  },
  getList:function(){
    if (!this.data.canGetList){
      return;
    }
    var that = this,
      url = 'index/discover',
      dataObj = {
        page:that.data.page
      };
    this.setData({
      canGetList:false
    })
    app.request(url, dataObj, function (data) {
      if (data.data.data.imgUrl){
        that.setData({
          imgUrl: data.data.data.imgUrl
        })
      }
      if (data.data.data.discoverList.length>0) {
        that.setData({
          findData: that.data.findData.concat(data.data.data.discoverList),
          page:that.data.page+1
        })
      }
      that.setData({
        canGetList:true,
      })
    })
  },
  reachBottom:function(){
    this.getList();
  },
  //事件处理函数
  open: function (evt) {
    let that = this;
    var openId = wx.getStorageSync('openid');
    var item = evt.currentTarget.dataset.item;
    console.log(item);
    if (!!item.qr_image) {
      return wx.previewImage({
        urls: [item.qr_image],
      })
    }
    var url = 'Index/add_click_action',
    dataObj = {
      type: item.type,
      typeId: !!item['banner_id'] ? item['banner_id'] : item['game_id'],
      trace: !!wx.getStorageSync('trace') ? wx.getStorageSync('trace') : ''
    };
    app.request(url,dataObj,function(data){
      var extrad = item.extraData;
      var path = data.path;
      if (typeof extrad == "string") {
        extrad = JSON.parse(extrad);
      }
      console.log(item.path.indexOf("?") != -1 ? item.path : 'pages/index/index?' + item.path)
      wx.navigateToMiniProgram({
        appId: item.app_id,
        path: item.path.indexOf("?") != -1 ? item.path : 'pages/index/index?' + item.path,
        extraData: extrad,
        envVersion: 'release',
        success(res) {
          console.log(res);
          // 打开成功
        }
      });
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log(ops.target)
    }
    return {
      title: '海盗游戏盒子',
      path: '/pages/index/index?parent_id=' + wx.getStorageSync('uid'),
      imageUrl: this.data.imgUrl ? this.data.imgUrl : '',
      success: function (res) {
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }
  }
})